var getInitStatic = getInitStatic();
$(function(){
    var optMoney = getInitStatic['optMoney'];
    var ds = "";
    var flag = 0;
    var fromtype = getInitStatic['fromtype'];
    var moneyType = 2;
    var cid = getInitStatic['cid'];

    function createWxQR(total_fee,fromtype,moneyType,cid){
        $.ajax({
            type:"post",
            url:"http://ibaotu.com/?m=weixinPay&a=renderWxQR",
            data:{type:total_fee,fromtype:fromtype,moneyType:moneyType,cid:cid},
            dataType:"json",
            success:function(res) {
                if (res.status == 0) {
                    showLogin('登录轰轰网', '请先登录');
                } else if (res.status != 500) {
                    $("#wxqr img").attr("src", res.code_img_url);
                    if (flag < 300) {
                        ds = setInterval(function() {
                            ajaxWxCallback(res.order_no, flag,total_fee);
                            flag = flag + 2;
                        }, 3e3);
                    } else if (flag >= 300 && flag < 600) {
                        ds = setInterval(function() {
                            ajaxWxCallback(res.order_no, flag,total_fee);
                            flag = flag + 2;
                        }, 1e4);
                    }
                }
            }
        });
    }

    function ajaxWxCallback(order_no, flag,total_fee) {
        if (flag >= 600) {
            clearInterval(ds);
        }
        $.ajax({
            type:"post",
            url:"http://ibaotu.com/?m=weixinPay&a=wxNotify",
            data:"order_no=" + order_no,
            dataType:"json",
            success:function(call_res) {
                if (call_res.status == true) {
                    clearInterval(ds);

                    sa.track('recharge', {
                        'rechargeType': '个人VIP',
                        'vipType': String(total_fee),
                        'rechargeAmount': Number(total_fee),
                        'paymentMethod': '微信',
                        'isSuccess': Boolean(true)
                    })

                    window.location.href = "/?m=vip&a=success&money="+total_fee+"&uid="+getInitStatic['uid'];
                }
            }
        });
    }

    createWxQR(optMoney,fromtype,moneyType,cid);

    $(".recharge-m").on('click', 'a', function(){
        $(this).addClass("this-card").siblings().removeClass("this-card");
        var amount = parseInt($(this).attr('amount'));
		var leixing = parseInt($(this).attr('leixing'));
        $('#type-amount').text(amount);
		$('.shengjileixing').attr("value",leixing);
        createWxQR(amount,fromtype,moneyType,cid);
    })

    $('.zhifubao-pay').on('click', 'div', function(){
        var auth_id = cookie('auth_id');
        if(auth_id == ""){
            showLogin('登录轰轰网', '请先登录');
            return false;
        }
        var type = parseInt($('#type-amount').text());
        $.ajax({
            type: 'post',
            url: 'http://ibaotu.com/?m=vip&a=pay',
            data: {type:type,fromtype:fromtype,moneyType:moneyType,cid:cid},
            dataType: 'json',
            success: function(r){
                if(r.status == 1){
                    window.location.href = r.link;
                }else if(r.status == 0){
                    alert(r.msg);
                }
            }
        });
    });

    $('#pay_s2').on('click', function(){
        var auth_id = cookie('auth_id');
        if(auth_id == ""){
            showLogin('登录轰轰网', '请先登录');
            return false;
        }
        var type = parseInt($('#type-amount').text());
        $.ajax({
            type: 'post',
            url: 'http://ibaotu.com/?m=vip&a=pay',
            data: {type:type,fromtype:fromtype,moneyType:moneyType,cid:cid},
            dataType: 'json',
            success: function(r){
                if(r.status == 1){
                    window.location.href = r.link;
                }else if(r.status == 0){
                    alert(r.msg);
                }
            }
        });
    });
})